/*
 Copyright (c) 2008  Seth Bourget  <seth@cafesilencio.net>
 All rights reserved.

 Licensed under the Apache License, Version 2.0 (the "License"); 
 you may not use this file except in compliance with the License. 
 You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
 Unless required by applicable law or agreed to in writing, 
 software distributed under the License is distributed on an "AS IS" BASIS, 
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 See the License for the specific language governing permissions and limitations under the License. 

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

version 1.0
*/
package net.cafesilencio.awssimpledb
{
	import com.hurlant.util.Base64;
	
	import flash.events.Event;
	
	public class SimpleDBEvent extends Event
	{
		
		public static const DOMAIN_CREATED:String 		= "simpleDBDomainCreatedEvent";
		public static const DOMAIN_DELETED:String 		= "simpleDBDomainDeletedEvent";
		public static const DOMAINS_LISTED:String 		= "simpleDBDomainsListedEvent";
		public static const ATTRIBUTES_PUT:String 		= "simpleDBPutAttributesEvent";
		public static const ATTRIBUTES_RECEIVED:String 	= "simpleDBGetAttributesEvent";
		public static const ATTRIBUTES_REMOVED:String 	= "simpleDBRemoveAttributesEvent";		
		public static const QUERY_COMPLETED:String 		= "simpleDBQueryCompleteEvent";
		public static const AWS_SIMPLEDB_ERROR:String 	= "simpleDBErrorEvent";
		public static const QUERY_WITH_ATTRIBUTES_COMPLETED:String 	= "simpleDBQueryWithAttributesCompleteEvent";
		public static const SIMPLEDB_CREDENTIALS_NOT_SET:String 	= "simpleDBCredentialsNotSetEvent";
		public static const DOMAIN_METADATA_RECEIVED:String			= "simpldeDBDomainMetaDataReceivedEvent";
		public static const QUERY_WITH_SQL_EXPRESSION_COMPLETED:String = "simpleDBQueryWithSQLExpressionCompleteEvent";
		
		private namespace amazonSimpleDBNS = "http://sdb.amazonaws.com/doc/2007-11-07/";
		
		public var rawAmazonResponse:XML = null;
		public var errorText:String = "";
		
		/*********************************************************************/
		
		public function SimpleDBEvent(type:String,bubbles:Boolean = false,cancelable:Boolean = false)
		{
			super(type,bubbles,cancelable);
		}
		

		
		/*********************************************************************/
		
		public function getRequestBoxUsage():String
		{
			use namespace amazonSimpleDBNS;
			if(rawAmazonResponse != null)
			{
				if(this.type == AWS_SIMPLEDB_ERROR)
					return rawAmazonResponse.Errors.Error.BoxUsage;
				else
					return rawAmazonResponse.ResponseMetadata.BoxUsage;
			}
			else
				return null;
		}
		
		/*********************************************************************/
		
		public function getErrorCode():String
		{
			if(rawAmazonResponse != null && this.type == AWS_SIMPLEDB_ERROR)
				return rawAmazonResponse.Errors.Error.Code;
			else
				return errorText;
		}
		
		/*********************************************************************/
		
		public function getErrorMessage():String
		{
			if(rawAmazonResponse != null && this.type == AWS_SIMPLEDB_ERROR)
				return rawAmazonResponse.Errors.Error.Message;
			else
				return null;			
		}
		
		/*********************************************************************/
		
		public function getListOFDomains():Array
		{
			var domainListArr:Array = new Array();
			if(rawAmazonResponse != null && this.type == DOMAINS_LISTED)
			{				
				use namespace amazonSimpleDBNS;
				var domainList:XMLList = rawAmazonResponse.ListDomainsResult.DomainName;
				
				for each(var item:Object in domainList)
					domainListArr.push(item.toString());
			}
			
			return domainListArr;
		}
		
		/*********************************************************************/
		
		public function getAttributesFromRequest():Array
		{
			var objectsToReturn:Array = new Array();
			
			if(rawAmazonResponse != null && this.type == ATTRIBUTES_RECEIVED)
			{
				use namespace amazonSimpleDBNS;
				
				var attribList:XMLList = rawAmazonResponse.GetAttributesResult.Attribute;
				for each(var attrib:XML in attribList)
				{
					var attribName:String = attrib.Name.toString();
					var attribValue:String = attrib.Value.toString();
					
					if(attrib.Name.@encoding == "base64")
						attribName = Base64.decode(attribName);
						
					if(attrib.Value.@encoding == "base64")
						attribValue = Base64.decode(attribValue);
					
					objectsToReturn.push(new SimpleDBAttributeObject(attribName,attribValue));
				}
			}
			
			return objectsToReturn;
		}

	}
}